<?php

function xmlheader() {
    static $isshowed;
    if (!$isshowed) {
        @header("Content-type: application/xml");
        echo "<?xml version=\"1.0\" encoding=\"utf-8\"?><document>";
        $isshowed = true;
    }
    return true;
}

function xmlfooter($halt = true) {
    echo "</document>";
    $halt && exit();
}

function check_verification($appkey_info, $data_format = '') {
    require_once 'api_key.php';
    $info = explode('_', $appkey_info);
    $temp = $info[0] . '_' . $info[1] . '_' . md5($info[1] . $public_key_flag[$info[0]]);
    if ($temp != $appkey_info) {
        errmsg(-1, 'usid验证失败', $data_format);
    } else {
        return $info;
    }
}

function check_verification_for_login($appkey_info, $data_format = '') {
    require_once 'api_key.php';
    $info = explode('_', $appkey_info);
    $temp = $info[0] . '_' . $info[1] . '_' . md5($info[1] . $public_key_flag[$info[0]]);
    if ($temp != $appkey_info) {
        return "";
    } else {
        return $info;
    }
}

function check_godson() {
    require_once 'api_key.php';

    $key = $_REQUEST['key'];
    $time = $_REQUEST['time'];
    $ks = md5($public_key_flag['10003'] . $time);
    if ($key != $ks) {
        errmsg('-1', 'usid验证失败');
    }
}

function error_login($data_format = '') {
    errmsg(-1, 'usid验证失败', $data_format);
}

function errmsg($error, $errmsg, $data_format = '') {
    if ($_REQUEST['data_format'] == 'json' || $data_format == 'json') {
        die("{code:$error,message:$errmsg}");
    } else {
        xmlheader();
        echo '<code>' . $error . "</code><message>" . $errmsg . "</message>";
        xmlfooter();
    }
}

//新建上传图片，附件目录
function upload_attachment($directory = "/openapi/homework/upload") {
    $img_dir = $_SERVER['DOCUMENT_ROOT'] . $directory; //上传img文件地址 采用绝对地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"

    $year = date('Ym', time());
    if (!is_dir($img_dir . '/' . $year)) {
        mkdir($img_dir . '/' . $year, 0777);
    }
    $day = date('d', time());
    if (!is_dir($img_dir . '/' . $year . '/' . $day)) {
        mkdir($img_dir . '/' . $year . '/' . $day, 0777);
    }

    return $img_dir = $_SERVER['DOCUMENT_ROOT'] . $directory . '/' . date('Ym', time()) . '/' . date('d', time());
}

/**
  +----------------------------------------------------------
 * XML转成数组  www.plframe.com
  +----------------------------------------------------------
 * @param string  $xml 变量 xml内容 || xml文件路径
  +----------------------------------------------------------
 * @return array
  +----------------------------------------------------------
 */
function xmlToArray($xml, $section = '') {
    if (!$xml) {
        return array();
    }

    if (strstr($xml, '<?xml')) {
        $xml = simplexml_load_string($xml);
    } else if (is_file($xml)) {
        $xml = simplexml_load_file($xml);
    } else {
        return array();
    }

    if ($section && isset($xml->$section)) {
        $xml = $xml->$section;
    }

    return object_toArray($xml);
}

/**
  +----------------------------------------------------------
 * object 转成数组   www.plframe.com
  +----------------------------------------------------------
 * @param string  $object 对象
  +----------------------------------------------------------
 * @return array
  +----------------------------------------------------------
 */
function object_toArray($object) {
    $rtn = NULL;
    if (is_array($object)) {
        foreach ($object as $key => $value) {
            $rtn[$key] = object_toArray($value);
        }
    } else {
        $var = get_object_vars($object);
        if ($var) {
            foreach ($var as $key => $value) {
                $rtn[$key] = object_toArray($value);
            }
        } else {
            return $object;
        }
    }
    return $rtn;
}

/* // 上传XML中的图片到指定位置

  $pic_info   base64位的编码
  $url_path  存放图片地址 /data/attachment/homework  后面不加/
  返回图片的地址
 */

function upload_xml_pic($pic_base64_info, $url_path = '/data/attachment/homework', $ext = '.gif') {
    $content = $pic_base64_info;
    $data = base64_decode($content);
    $temp_name = date("YmdHis") . rand(1, 9999) . $ext;

    $upload_dect = upload_attachment($directory = $url_path);

    file_put_contents($upload_dect . '/' . $temp_name, $data);

    return $upload_dect . '/' . $temp_name;
}

// 获取文件的后缀名

function filekzm($a) {
    $c = strrchr($a, '.');
    if ($c) {
        return $c;
    } else {
        return '';
    }
}

?>